#include "LinuxFuse.h"

LinuxFuse::LinuxFuse(const char* ip, const char* port) {
    client = new TCPClient(ip, port);
}

/*
   Currently only the type bits of st_mode and possibly st_ino (if
   '-ouse_ino' was specified) are used.  These are then used to fill in
   the d_type and d_ino fields of 'struct dirent' returned by readdir(3).

   But there are plans to use the other fields for a future
   readdirplus(3) interface and/or pre-caching the attributes.

   So basically, it's OK to pass NULL, but if it's not costly to fill in
   the attributes, then it doesn't hurt to do so.
 */
std::vector<std::string> LinuxFuse::getFilesInDir(const char *path) {
    syslog(LOG_NOTICE, "client->getFilesInDir()");
    return client->getFilesInDir(path);
}


//struct stat {
//dev_t     st_dev;     /* ID of device containing file */
//ino_t     st_ino;     /* inode number */
//mode_t    st_mode;    /* protection */
//nlink_t   st_nlink;   /* number of hard links */
//uid_t     st_uid;     /* user ID of owner */
//gid_t     st_gid;     /* group ID of owner */
//dev_t     st_rdev;    /* device ID (if special file) */
//off_t     st_size;    /* total size, in bytes */
//blksize_t st_blksize; /* blocksize for file system I/O */
//blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
//time_t    st_atime;   /* time of last access */
//time_t    st_mtime;   /* time of last modification */
//time_t    st_ctime;   /* time of last status change */
//};

int LinuxFuse::getFileAttribute(const char *path, struct stat *stbuf) {

    syslog(LOG_NOTICE, "getattr: %s", path);
    std::string str(path);
    ResponseAttribute* atribute = (ResponseAttribute*) client->getAttributes(path);

    memset(stbuf, 0, sizeof (struct stat));
    if (atribute->status != 0) {
        return -ENOENT;
    }

    (stbuf)->st_nlink = 1;
    (stbuf)->st_size = atribute->size;

    if (atribute->type == 0) {
        (stbuf)->st_mode = S_IFDIR | 0755;
    } else {
        (stbuf)->st_mode = S_IFREG | 0444;
    }

    (stbuf)->st_atime = time(NULL);
    (stbuf)->st_mtime = time(NULL);
    (stbuf)->st_ctime = time(NULL);
    return 0;
}

int LinuxFuse::read(const char *path, char *buf, size_t size, off_t offset) {
    size_t len = strlen(path);
    if (offset < len) {
        if (offset + size > len)
            size = len - offset;
        memcpy(buf, path + offset, size);
    } else
        size = 0;
    return size;
}

int LinuxFuse::openFile(const char* path)
{


}